<template>
  <div>
    <!-- 面包屑导航  这个应该写成一个模板的 --> 
    <navigation-bar></navigation-bar>
    <el-card>
      <el-row :gutter="25">
        <el-col :span="10">
          <!-- 搜索添加 -->
          <el-input placeholder="请输入搜索内容" v-model="queryInfo.query" clearable @clear="getUserList">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>

        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表 -->
      <el-table :data="productList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="productName"></el-table-column>
        <el-table-column label="商品价格" prop="price"></el-table-column>
        <el-table-column label="商品日期" prop="productDate"></el-table-column>
        <el-table-column label="商品Id" prop="id"></el-table-column>
        <el-table-column label="状态" prop="state">
          <!-- scope.row 就是当前行的信息 -->
          <template slot-scope="scope">
            <el-switch v-model="scope.row.state" @change="userStateChanged(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteUser(scope.row.id)"></el-button>
            <!-- 权限 -->
            <el-tooltip effect="dark" content="分配权限" placement="top-start" :enterable="false">
              <el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
         @size-change="handleSizeChange" 
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 创建新用户对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%"
    @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm" :rules="addFormRules"
        ref="addFormRef" label-width="70px">
        <!-- 用户名 -->
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="addForm.productName"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="价格" prop="price">
          <el-input v-model="addForm.price"></el-input>
        </el-form-item>
         <el-form-item label="简介" prop="info">
          <el-input v-model="addForm.info"></el-input>
        </el-form-item>
         <el-form-item label="日期" prop="productDate">
          <el-input v-model="addForm.productDate"></el-input>
        </el-form-item>
        <el-form-item label="货号" prop="id">
          <el-input v-model="addForm.id"></el-input>
        </el-form-item>
         <el-form-item label="状态" prop="state">
          <el-input v-model="addForm.state"></el-input>
        </el-form-item>
      </el-form>
      <!-- 内容底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addProduct">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户对话框 -->
    <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="50%" @colse="editDialogClosed">
       <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
       <el-form-item label="商品名称" prop="productName">
          <el-input v-model="editForm.productName"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="价格" prop="price">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
         <el-form-item label="简介" prop="info">
          <el-input v-model="editForm.info"></el-input>
        </el-form-item>
         <el-form-item label="日期" prop="productDate">
          <el-input v-model="editForm.productDate"></el-input>
        </el-form-item>
        <el-form-item label="货号" prop="id">
          <el-input v-model="editForm.id"></el-input>
        </el-form-item>
         <el-form-item label="状态" prop="state">
          <el-input v-model="editForm.state"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import NavigationBar from '../views/NavigationBar.vue';
export default {
  components: { NavigationBar },
   
  data() {
    return {
      // 请求数据
      queryInfo: {
        methods: "",
        pageNum: 1,
        pageSize: 5
      },
      productList: [],// 用户列表
     
      total: 0, // 最大数据记录
      addDialogVisible: false ,// 对话框显示
      // 添加用户表单项
      addForm: {
        productName:'',
        info:'',
        id:'',
        price:'',
        state:'',
        productDate:''
    
      }, 
      // 控制修改用户对话框显示/隐藏
      editDialogVisible:false,
      // 修改用户信息
      editForm:{
        // productName:'',
        // info:'',
        // id:'',
        // price:'',
        // state:'',
        // productDate:''
      },
      // 验证规则
      addFormRules:{
        productName:[
           { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 0, max: 8, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        info:[
           { required: true, message: "请输入密码", trigger: "blur" },
          { min: 0, max: 8, message: "长度在 6 到 8 个字符", trigger: "blur" }
        ],
        id:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
         price:[
           { required: true, message: "请输入价格", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
         state:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
         productDate:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
      },
      // 修改用户表单验证规则
      editFormRules:{
          productName:[
        { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 0, max: 15, message: "长度在 5 到 8 个字符", trigger: "blur" }
        ],
        info:[
           { required: true, message: "请输入密码", trigger: "blur" },
          { min: 0, max: 15, message: "长度在 6 到 8 个字符", trigger: "blur" }
        ],
        id:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
         price:[
           { required: true, message: "请输入价格", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
         state:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        ],
         productDate:[
           { required: true, message: "请输入邮箱", trigger: "blur" },
          { min: 0, max: 15, message: "请输入正确邮箱地址", trigger: "blur" }
        
          ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      // 调用post请求
      this.queryInfo.methods='allProduct'
      const { data: res } = await this.$http.get("product", {
        params: this.queryInfo
      });
      this.productList = res.data; // 将返回数据赋值
      this.total = res.numbers; // 总个数
    },
    // 监听pageSize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 监听pageNum改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getUserList(); // 数据发生改变重新申请数据
    },
    // 修改用户状态
    async userStateChanged(userinfo) {
      const { data: res } = await this.$http.get(   `product?methods=updateState&id=${userinfo.id}&state=${userinfo.state}`);
      if (res != "success") {
        userinfo.state = !userinfo.state;
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
    },
    // 监听添加用户
    addDialogClosed(){
      this.$refs.addFormRef.resetFields();// 重置表单项
    },
    // 添加用户
    addProduct(){
        this.$refs.addFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        if(this.addForm==null){
            return this.$message.error("kong！");
        }
        // 发起请求
        const {data:res} = await this.$http.post("product?methods=addProduct",this.addForm);
        if (res != "success") {
        // userinfo.state = !userinfo.state;
        return this.$message.error("操作失败！！！");
        }
        this.$message.success("操作成功！！！");
        //隐藏
        this.addDialogVisible = false;
        this.getUserList();
      })
    },
    // 展示修改框
    async showEditDialog(id){
       
        const {data:res} = await this.$http.get("product?methods=getUpdateProduct&id="+id);
        // if (res != "success") {
        // userinfo.state = !userinfo.state;
        // return this.$message.error("操作失败！！！");
        // }
        // this.$message.success("操作成功！！！");

        this.editForm = res;
        this.editDialogVisible = true;
    },
    // 关闭窗口
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    // 确认修改
    editUserInfo(){
      this.$refs.editFormRef.validate(async valid =>{
        console.log(valid);
        if( !valid ) return;
        // 发起请求
        const {data:res} = await this.$http.post("product?methods=editProduct",this.editForm);
        console.log(res);
         if (res != "success") return this.$message.error("操作失败！！！");
        this.$message.success("操作成功！！！");
        //隐藏
        this.editDialogVisible = false;
        this.getUserList();
      });
    },
    // 删除按钮
    async deleteUser(id){
      // 弹框
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
      // 成功删除为confirm 取消为 cancel
      if(confirmResult!='confirm'){
        return this.$message.info("已取消删除");
      }
      const {data:res} = await this.$http.get("product?methods=deleteProduct&id="+id);
      if (res != "success") {
        return this.$message.error("操作失败！！！");
      }
      this.$message.success("操作成功！！！");
      this.getUserList();
    },
  }
};
</script>
<style lang="less" scoped>
</style>